# -*- coding: utf-8 -*-
"""
file:ch6_5定积分求二次函数
author: Fuwen
time: 2022/7/20 6:40
software:PyCharm
"""
import numpy as np
import matplotlib.pyplot as plt

# 原函数为二次函数 y=2*x**2 微分 f(x) = x, 积分区间为[-1,1] 定积分为求和， 即在积分区间内f(x)的微分和，
# 需要求 ∫|x|dx,  x∈[-1, 1]

# 被积函数系数
a = -1

# 分区间求定积分 [-1,0]
x = np.linspace(-2, 0, 100)
y = a * x
plt.plot(x, y, )
plt.fill_between(x, y1=y, y2=0, where=(x>=-1)&(x<=0), facecolor='lightgreen')

# 分区间求定积分 [0, 1]
x = np.linspace(0, 2, 100)
y = -a * x
plt.plot(x, y)
plt.fill_between(x, y1=y, y2=0, where=(x>=0)&(x<=1), facecolor='lightblue')

plt.grid()
plt.show()